package baidu.com.matedemo.base;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;

import com.baidu.duer.libcore.api.IBdussExpiredCallback;
import com.baidu.duer.libcore.error.OnCrashHandle;
import com.baidu.duer.libcore.error.UncatchExceptionHandler;
import com.baidu.duer.libcore.util.ConsoleLogger;
import com.baidu.duer.smartmate.out.DuerSDK;
import com.baidu.duer.smartmate.out.oauth.ILoginCallback;

import java.lang.ref.WeakReference;

/**
 * Created by niufei01 on 17/3/15.
 */

public class DemoApplication extends Application implements IBdussExpiredCallback {
    private static Context appContext;
    //为避免内存泄漏使用弱引用
    private WeakReference<Activity> mCurrentActivity;

    @Override
    public void onCreate() {
        super.onCreate();
        appContext = this;

        DuerSDK.initialize(this);

        new UncatchExceptionHandler().init(this, new OnCrashHandle() {
            @Override
            public void crashHandle(Throwable ex) {
                ConsoleLogger.printException(DemoApplication.class, ex);
            }
        });

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
            registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
                @Override
                public void onActivityCreated(Activity activity, Bundle bundle) {
                    //do nothing
                }

                @Override
                public void onActivityStarted(Activity activity) {
                    //do nothing
                }

                @Override
                public void onActivityResumed(Activity activity) {
                    mCurrentActivity = new WeakReference<>(activity);
                }

                @Override
                public void onActivityPaused(Activity activity) {
                    //do nothing
                }

                @Override
                public void onActivityStopped(Activity activity) {
                    //do nothing
                }

                @Override
                public void onActivitySaveInstanceState(Activity activity, Bundle bundle) {
                    //do nothing
                }

                @Override
                public void onActivityDestroyed(Activity activity) {
                    //do nothing
                }
            });
        }
    }

    public static Context getAppContext() {
        return appContext;
    }

    @Override
    public void onTerminate() {
        super.onTerminate();
        DuerSDK.uninitialize(this);
    }

    @Override
    public void bdussHasExpired(int i, String s) {
        // SDK内部维护了百度Token来请求相关服务，如果百度token失效，会回调此接口，开发者需要重新跳转授权页面
        if (mCurrentActivity != null) {
            DuerSDK.login(mCurrentActivity.get(), new ILoginCallback() {
                @Override
                public void onCancel() {

                }

                @Override
                public void onError(int code, String message) {

                }

                @Override
                public void onCompleted() {

                }
            });
        }
    }
}
